using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System;

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Callbacks;
using UnityEditor.Android;
#endif


#if UNITY_IOS || UNITY_STANDALONE_OSX
using UnityEditor.iOS.Xcode;
#endif

/// <summary>
/// OnPreprocessBuild --> OnPostGenerateGradleAndroidProject -> OnPostprocessBuild
/// </summary>

public class BuildProcessUtil : Editor, IPostGenerateGradleAndroidProject, IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
    public int callbackOrder => 0;

    /// <summary>
    /// 打包前的处理
    /// </summary>
    void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
    {
        switch (report.summary.platform)
        {
            case BuildTarget.iOS:
                OnIosProjectPreprocessBuild(report);
                break;
            case BuildTarget.Android:
                OnAndroidProjectPreprocessBuild(report);
                break;
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                OnWindowsProjectPreprocessBuild(report);
                break;
            case BuildTarget.StandaloneOSX:
                OnMacProjectPreprocessBuild(report);
                break;
            default:
                break;
        }
    }

    void OnIosProjectPreprocessBuild(BuildReport report)
    {
#if UNITY_IOS
        PlayerSettings.allowedAutorotateToPortrait = true;
#endif
    }

    void OnAndroidProjectPreprocessBuild(BuildReport report)
    {
        PlayerSettings.allowedAutorotateToPortrait = false;
    }

    void OnWindowsProjectPreprocessBuild(BuildReport report)
    {
    }

    void OnMacProjectPreprocessBuild(BuildReport report)
    {
    }

    /// <summary>
    /// 打包后的处理
    /// </summary>
    /// <param name="report"></param>
    void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
    {
        switch (report.summary.platform)
        {
            case BuildTarget.iOS:
                onExportIosProjectBuildProcess(report);
                break;
            case BuildTarget.Android:
                onExportAndroidProjectBuildProcess(report);
                break;
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                onExportWindowsProjectBuildProcess(report);
                break;
            case BuildTarget.StandaloneOSX:
                onExportMacProjectBuildProcess(report);
                break;
            default:
                break;
        }
    }

    /// <summary>
    /// 导出ios工程后的处理
    /// </summary>
    /// <param name="report"></param>
    void onExportIosProjectBuildProcess(BuildReport report)
    {
#if UNITY_IOS
        // Debug.Log("导出工程后的处理");
        // Debug.Log("Output Path:" + report.summary.outputPath);
        // if (!Directory.Exists(report.summary.outputPath))
        // {
        //     Debug.LogError("导出工程的输出文件夹不存在:" + report.summary.outputPath);
        //     return;
        // }

        // string outputPath = report.summary.outputPath;
        // string iosDirPath = Path.GetDirectoryName(report.summary.outputPath);

        // //删除部分不需要的文件
        // string deleteFilePath1 = Path.Combine(outputPath, "Libraries", "IL2CPPOptions.cpp.cpp");
        // if (File.Exists(deleteFilePath1))
        // {
        //     File.Delete(deleteFilePath1);
        // }
        // string srcFileName = Path.Combine(outputPath, "Libraries","baselib.a");
        // string destFileName = Path.Combine(outputPath, "Libraries","libbaselib.a");
        // if (File.Exists(srcFileName))
        // {
        //     File.Move(srcFileName, destFileName);
        // }
#endif
    }

    /// <summary>
    /// 导出android工程后的处理
    /// </summary>
    /// <param name="report"></param>
    void onExportAndroidProjectBuildProcess(BuildReport report)
    {
    }

    /// <summary>
    /// 导出windows工程后的处理
    /// </summary>
    /// <param name="report"></param>
    void onExportWindowsProjectBuildProcess(BuildReport report)
    {
        Debug.Log("导出工程后的处理");
        Debug.Log("Output Path:" + report.summary.outputPath);

        string bat = "/copy_log.bat";
        string bat2 = "/fix_connect.bat";
        string outputPath = report.summary.outputPath;
        string outputPath2 = report.summary.outputPath;
        outputPath = outputPath.Substring(0, outputPath.LastIndexOf('/')) + bat;
        outputPath2 = outputPath.Substring(0, outputPath.LastIndexOf('/')) + bat2;

        string path = Application.dataPath.Replace('\\', '/');
        string path2 = path;
        path = path.Substring(0, path.LastIndexOf('/')) + "/Proto" + bat;
        path2 = path2.Substring(0, path2.LastIndexOf('/')) + "/Proto" + bat2;
        File.Copy(path, outputPath);
        File.Copy(path2, outputPath2);
    }

    /// <summary>
    /// 导出Mac工程后的处理
    /// </summary>
    /// <param name="report"></param>
    void onExportMacProjectBuildProcess(BuildReport report)
    {
    }

    public void OnPostGenerateGradleAndroidProject(string path)
    {

    }

    //除了exceptDir之外的文件夹，其他都拷贝过去//
    void CopyDirectoryWithFilter(string srcDir, string destDir, string exceptDir)
    {
    }
}
